package com.superman.CategoryServiceImpl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.superman.dao.CategoryDao;
import com.superman.dao.ProductDao;
import com.superman.daoImpl.CategoryDaoImpl;
import com.superman.domain.Category;
import com.superman.service.CategoryService;
import com.superman.serviceImpl.ProductDaoImpl;
import com.superman.utils.JDBCUtils;

public class CategoryServiceImpl implements CategoryService {
	private CategoryDao categoryDao = new CategoryDaoImpl();
	@Override
	public void add(Category category) {
		categoryDao.add(category);
	}
	@Override
	public List<Category> findAllCategory() {
		return categoryDao.findAllCategory();
	}
	private ProductDao productDao = new ProductDaoImpl();
	@Override
	public void remove(String cid) {
		Connection conn = null;
		try {
			conn = JDBCUtils.getConnection();
			conn.setAutoCommit(false);
			//开启事务
			productDao.release(conn,cid);
			//解除外键关系
			categoryDao.remove(conn,cid);
			//删除分类
			conn.commit();
			//提交事务
		} catch (Exception e) {
			e.printStackTrace();
			if (conn != null) {
				try {
					conn.rollback();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}finally{
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	@Override
	public Category findById(String cid) {
		return categoryDao.findById(cid);
	}
	@Override
	public void updateCategory(String cid,String cname) {
		categoryDao.updateCategory(cid,cname);
	}

}
